#coding=utf-8
from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _

# Create your models here.

class DebateCategory(models.Model):
    name = models.CharField(_('Name'), max_length=100, unique=True)
    description = models.TextField(_('Description'))
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name = _('Debate Category')
        db_table = 'debate_category'
    

class DebateType(models.Model):
    name = models.CharField(_('Name'), max_length=100, unique=True)
    description = models.TextField(_('Description'))
    small_desc = models.TextField(_('Small Description'))
    
    def __unicode__(self):
        return self.name
    
    class Meta:
        verbose_name = _('Debate Type')
        db_table = ['name']

class Debate(models.Model):
    title = models.CharField(_('Title'), max_length=100, unique=True, help_text=_("Debate Title|||Crafting a controversial debate title is crucial for a great debate.  The title will catch the user's eye and make them want to argue about this topic. Go ahead, be bold!"))
    
    tag = models.CharField(_('Tagline'), max_length=100, help_text=_("Let's make it interesting|||A strong tagline helps spice up the debate.  Be specific about what you want people to debate about, or add some humor to draw people to your debate."))
    
    desc = models.TextField(_('Description'), help_text=_("Clarify Your Debate|||Adding a description to a debate helps to clarify exactly what you want everyone to discuss.  Add a description to ensure the debate is focused on the issue at hand."))
    
    category = models.ForeignKey(DebateCategory, verbose_name=_('Category'), help_text=_("Choose a category so people can find it!|||You didn't set up the perfect debate for nothing, did you?  Make it easy for people to find your debate by putting it in the right category.  Users can filter by category to find what they're most interested in."))
    
    user = models.ForeignKey(User, verbose_name=_('user'))
    #结束时间
    ends = models.IntegerField(_('Ends Time'), choices=(0, _('Nerver'), (1, _('Specific Date'))), help_text=_("Keep CreateDebate Fresh!|||When do you want this debate to end?  Want it to go on forever?  Want a specific end date?  Customize as you like."))
    
    #当ends为1时有效
    spec_date = models.DateField(_('Specific Date'), blank=True)
    type = models.ForeignKey(DebateType, verbose_name=_('Debate Type'))
    #观点一和二，当type为2-side时有效
    side1 = models.CharField(_('View #1'), max_length=100)
    side2 = models.CharField(_('View #2'), max_length=100)
    #状态，指示此辩论是否已经结束
    state = models.IntegerField(_('State'), choices=((0, _('Open')), (1, _('Close'))))
    #总得分
    score = models.IntegerField(_('Score'), default=0, edit_able=False)
    #论点一得分
    score1 = models.IntegerField(_('Score1'), default=0, edit_able=False)
    #论点二得分
    score2 = models.IntegerField(_('Score2'), default=0, edit_able=False)
    #总投票数
    votes = models.IntegerField(_('Total Votes'), default=0, edit_able=False)
    #发布时间
    post_time = models.DateTimeField(_('Post Time'), auto_now_add=True)
    #修改时间
    edit_time = models.DateField(_('Edit Time'), auto_now=True)
    
    link = models.URLField(_('Link'), blank=True, help_text=_("Link your debate to the Web|||Creating a debate about a link is a great way to make the debate topical and specific.  The Focal Link will appear below the Debate Title and people will usually check it out before participating in the debate."))
    link_title = models.CharField(_('Title for link'), blank=True, help_text=_("Let's give that link/video a title|||If you made a link or video the focal part of the debate, add a catchy title here that will make debaters want to check out the link and argue in your debate."))
    
    def __unicode__(self):
        return self.title
    
    class Meta:
        verbose_name = _('Debate')
        db_table = 'debate'
        ordering = ['-post_time',]

class Arguments(models.Model):
    #相关论点
    debate = models.ForeignKey(Debate, verbose_name=_('Debate'))
    #是否支持该论点
    support = models.IntegerField(_('Support'), default=0, choices=((1, _('Support')), (0, _('Oppose'))))
    #论点内容
    argument = models.TextField(_('Argument'))
    #标签
    tag = models.CharField(_('Tag'), max_length=50, default='None')
    #外部链接，可以是视频之类的
    link = models.URLField(_('Link'), blank=True, help_text=_("Link your debate to the Web|||Creating a debate about a link is a great way to make the debate topical and specific.  The Focal Link will appear below the Debate Title and people will usually check it out before participating in the debate."))
    link_title = models.CharField(_('Title for link'), blank=True, help_text=_("Let's give that link/video a title|||If you made a link or video the focal part of the debate, add a catchy title here that will make debaters want to check out the link and argue in your debate."))
    
    #此论点得分
    score = models.IntegerField(_('Score'), default=0, edit_able=False)
    post_time = models.DateTimeField(_('Post Time'), auto_now_add=True)
    edit_time = models.DateField(_('Edit Time'), auto_now=True)
    
    user = models.ForeignKey(User, verbose_name=_('user'))
    
    #回复的论点ID
    parent = models.IntegerField(_('Parent Argument'), default=0)
    #当parent不为0时有效
    favor = models.IntegerField(_('Favor'), choices=((1, _('Favor')), (0, _('Oppose'))), default=0)
    
    
    def __unicode__(self):
        return self.argument[:20]
    
    class Meta:
        verbose_name = _('Arguments')
        db_table = 'arguments'
        ordering = ['-post_time',]
    
    
class Votes(models.Model):
    arguments = models.ForeignKey(Arguments, verbose_name=_('arguments'))
    user = models.ForeignKey(User, verbose_name=_('user'))
    score = models.IntegerField(_('Score'))
    
    def __unicode__(self):
        return '%s_%s_%s' % (self.user, self.arguments, self.score)
    
    class Meta:
        verbose_name = _('Votes')
        db_table = 'votes'
        